package com.eco.fanliapp.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author zhuchao
 * @create 2019-09-26 11:50
 */
public class DateCalculateUtils {

	static Logger logger = LoggerFactory.getLogger(DateCalculateUtils.class);

	/**
	 * 切割时间段
	 *
	 * @param dateType 交易类型 M/D/H/N -->每月/每天/每小时/每分钟
	 * @param amout 多长时间分成一份
	 * @param start yyyy-MM-dd HH:mm:ss
	 * @param end  yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static List<String> cutDate(String dateType, int amout, String start, String end) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date dBegin = sdf.parse(start);
			Date dEnd = sdf.parse(end);
			return findDates(dateType,amout, dBegin, dEnd);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public static List<String> jdcutDate(String dateType, int amout, String start, String end) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date dBegin = sdf.parse(start);
			Date dEnd = sdf.parse(end);
			return jdfindDates(dateType,amout, dBegin, dEnd);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public static List<String> findDates(String dateType,int amout, Date dBegin, Date dEnd) throws Exception {
		List<String> listDate = new ArrayList<>();
		Calendar calBegin = Calendar.getInstance();
		calBegin.setTime(dBegin);
		Calendar calEnd = Calendar.getInstance();
		calEnd.setTime(dEnd);
		while (calEnd.after(calBegin)) {
			switch (dateType) {
				case "M":
					calBegin.add(Calendar.MONTH, amout);
					break;
				case "F":
					calBegin.add(Calendar.MINUTE, amout);
					break;
				case "D":
					calBegin.add(Calendar.DAY_OF_YEAR, amout);break;
				case "H":
					calBegin.add(Calendar.HOUR, amout);break;
				case "N":
					calBegin.add(Calendar.SECOND, amout);break;
			}
			if (calEnd.after(calBegin))
				listDate.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calBegin.getTime()));
			else
				listDate.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calEnd.getTime()));
		}
		return listDate;
	}


	public static List<String> jdfindDates(String dateType,int amout, Date dBegin, Date dEnd) throws Exception {
		List<String> listDate = new ArrayList<>();
		Calendar calBegin = Calendar.getInstance();
		calBegin.setTime(dBegin);
		Calendar calEnd = Calendar.getInstance();
		calEnd.setTime(dEnd);
		while (calEnd.after(calBegin)) {
			switch (dateType) {
				case "M":
					calBegin.add(Calendar.MONTH, amout);
					break;
				case "F":
					calBegin.add(Calendar.MINUTE, amout);
					break;
				case "D":
					calBegin.add(Calendar.DAY_OF_YEAR, amout);break;
				case "H":
					calBegin.add(Calendar.HOUR, amout);break;
				case "N":
					calBegin.add(Calendar.SECOND, amout);break;
			}
			if (calEnd.after(calBegin))
				listDate.add(new SimpleDateFormat("yyyyMMddHHmm").format(calBegin.getTime()));
			else
				listDate.add(new SimpleDateFormat("yyyyMMddHHmm").format(calEnd.getTime()));
		}
		return listDate;
	}

}
